package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.stark.camera.kit.height.Altimeter;
import com.stark.camera.kit.height.AltimeterHelpDialog;
import com.stark.camera.kit.height.AltimeterRet;
import com.stark.camera.kit.height.AltimeterStep;
import com.stark.camera.kit.height.AltimeterType;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityDiastimeterBinding;
import stark.common.basic.utils.StkPermissionHelper;

public class DiastimeterActivity extends BaseAc<ActivityDiastimeterBinding> {


    @Override
    protected int onCreate() {
        return R.layout.activity_diastimeter;
    }

    @Override
    protected void initView() {
        mDataBinding.ivDiastimeterBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvDiastimeterTitle1.setOnClickListener(this);
        mDataBinding.tvDiastimeterTitle2.setOnClickListener(this);
        mDataBinding.ivDiastimeterComp.setOnClickListener(this);
        mDataBinding.tvPreStep.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvDiastimeterTitle1://未落地
                Altimeter.getInstance().setAltimeterType(AltimeterType.NOT_IN_GROUND);
                updateDiastimeterType();
                break;
            case R.id.tvDiastimeterTitle2://已落地
                Altimeter.getInstance().setAltimeterType(AltimeterType.IN_GROUND);
                updateDiastimeterType();
                break;
            case R.id.tvPreStep://上一步
                Altimeter.getInstance().executePre();
                break;
            case R.id.ivDiastimeterComp://完成
                Altimeter.getInstance().executeMeasure();
                break;
        }
    }

    @Override
    protected void initData() {
        getPermission();
    }

    //获取权限
    private void getPermission() {
        StkPermissionHelper.permission(Manifest.permission.CAMERA)
                .reqPermissionDesc(getString(R.string.get_camera_permission))
                .callback(new StkPermissionHelper.ACallback() {
                    @Override
                    public void onGranted() {
                        initMeasure();
                    }
                }).request();
    }

    private void initMeasure() {
        mDataBinding.cameraView.setLifecycleOwner(this);
        updateDiastimeterType();
        //初始化
        Altimeter.getInstance().startListenSensor();
        Altimeter.getInstance().setListener(new Altimeter.IListener() {
            @Override
            public void onStep(@NonNull AltimeterStep step) {
                mDataBinding.ivDiastimeterComp.setImageResource(R.drawable.ps);
                mDataBinding.tvPreStep.setVisibility(View.VISIBLE);
                if (step == AltimeterStep.GROUND) {
                    mDataBinding.tvDiastimeterText.setText(R.string.step_text1);
                    mDataBinding.tvPreStep.setVisibility(View.GONE);
                } else if (step == AltimeterStep.BOTTOM) {
                    if (Altimeter.getInstance().getAltimeterType() == AltimeterType.NOT_IN_GROUND) {
                        mDataBinding.tvDiastimeterText.setText(R.string.step_text2);
                    } else {
                        mDataBinding.tvDiastimeterText.setText(R.string.step_in_ground_text1);
                        mDataBinding.tvPreStep.setVisibility(View.GONE);
                    }
                } else if (step == AltimeterStep.TOP) {
                    if (Altimeter.getInstance().getAltimeterType() == AltimeterType.NOT_IN_GROUND) {
                        mDataBinding.tvDiastimeterText.setText(R.string.step_text3);
                    } else {
                        mDataBinding.tvDiastimeterText.setText(R.string.step_in_ground_text2);
                    }
                    mDataBinding.ivDiastimeterComp.setImageResource(R.drawable.akscl);
                }
            }

            @Override
            public void onResult(@NonNull AltimeterRet ret) {
                MeasureResultActivity.ret = ret;
                startActivity(MeasureResultActivity.class);
            }
        });
        //默认身高
        Altimeter.getInstance().setHumanHeight(170);
//        Altimeter.getInstance().setAltimeterType(AltimeterType.IN_GROUND);
//        Altimeter.getInstance().setAltimeterType(AltimeterType.NOT_IN_GROUND);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Altimeter.getInstance().stopListenSensor();
    }


    private void updateDiastimeterType() {
        AltimeterType type = Altimeter.getInstance().getAltimeterType();
        if (type == AltimeterType.IN_GROUND) {
            mDataBinding.tvDiastimeterTitle1.setTextColor(Color.parseColor("#8FA5F8"));
            mDataBinding.tvDiastimeterTitle2.setTextColor(Color.parseColor("#FFFFFF"));
            mDataBinding.tvDiastimeterTitle1.setBackground(null);
            mDataBinding.tvDiastimeterTitle2.setBackgroundResource(R.drawable.shape_diastimeter_title_sel_bg);
        } else {
            mDataBinding.tvDiastimeterTitle1.setTextColor(Color.parseColor("#FFFFFF"));
            mDataBinding.tvDiastimeterTitle2.setTextColor(Color.parseColor("#8FA5F8"));
            mDataBinding.tvDiastimeterTitle1.setBackgroundResource(R.drawable.shape_diastimeter_title_sel_bg);
            mDataBinding.tvDiastimeterTitle2.setBackground(null);
        }
    }

}